// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database();
    const _ = db.command;
    const activityIds = event.activityIds;
    const activities = await db.collection('activity').aggregate()
        .match({
            _id: _.in(activityIds)
        })
        .lookup({
            from: 'user',
            localField: 'creatorId',
            foreignField: 'id',
            as: 'creator'
        })
        .lookup({
            from: 'user',
            localField: 'joiners.id',
            foreignField: 'id',
            as: 'users'
        })
        .sort({
            time: -1,
        })
        .end();
    return activities.list.map((activity) => {
        activity.creator = activity.creator[0];
        return activity;
    });
}
